package devcamp.teama.yahooooooo.dao;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.JDOUserException;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.Transaction;

import devcamp.teama.yahooooooo.dto.LocationDto;
import devcamp.teama.yahooooooo.dto.RegistrationDto;
import devcamp.teama.yahooooooo.util.ParameterUtils;

public class YahoooooooDao {

    PersistenceManager PersistenceManager;


    public void insert(RegistrationDto dto) {
        PersistentetService();
        Transaction transaction = PersistenceManager.currentTransaction();
        transaction.begin();
        try {
            PersistenceManager.makePersistent(dto);
            transaction.commit();
        } finally {
            if (transaction.isActive()) {
                transaction.rollback();
            }
            PersistenceManager.close();
        }
    }

    public void insert(LocationDto dto) {
        PersistentetService();
        Transaction transaction = PersistenceManager.currentTransaction();
        transaction.begin();
        try {
            PersistenceManager.makePersistent(dto);
            transaction.commit();
        } finally {
            if (transaction.isActive()) {
                transaction.rollback();
            }
            PersistenceManager.close();
        }
    }

    @SuppressWarnings("unchecked")
    public List<LocationDto> findUserById(Float latitude, Float longitude) {
        Float mLatitude = latitude - ParameterUtils.DIFF_LATTITUDE;
        Float pLatitude = latitude + ParameterUtils.DIFF_LATTITUDE;
        Float mLongitude = longitude - ParameterUtils.DIFF_LONGITUDE;
        Float pLongitude = longitude + ParameterUtils.DIFF_LONGITUDE;
        List<LocationDto> resultList = new ArrayList<LocationDto>();
        List<LocationDto> result = null;
        PersistentetService();
        try {
            Query query = PersistenceManager.newQuery(LocationDto.class);
            query.setFilter("latitude >= mLatitude && latitude<=pLatitude");
            query.setFilter("longitude >= mLongitude && longitude <= pLongitude");
            query.declareParameters("Float mLatitude, Float pLatitude, Float mLongitude,Float pLongitude");
            //            query.declareParameters("Float mLatitude, Float pLatitude, Float mLongitude,Float pLongitude");
            result = (List<LocationDto>) query.executeWithArray(mLatitude, pLatitude, mLongitude, pLongitude);
            PersistenceManager.detachCopyAll(result);
            for (LocationDto dto : result) {
                resultList.add(dto);
            }
        } catch (JDOUserException e) {
            // FIXME:ログの出力
            e.printStackTrace();
        } catch (Exception e) {
            // FIXME:ログの出力
            e.printStackTrace();
        } finally {
            PersistenceManager.close();
        }

        return resultList;
    }

    public RegistrationDto findUserById(Long id) {
        PersistentetService();
        try {
            RegistrationDto result = PersistenceManager.getObjectById(RegistrationDto.class, id);
            return result;
        } catch (JDOObjectNotFoundException e) {
            return null;
        }
    }

    @SuppressWarnings("unchecked")
    public List<RegistrationDto> findAll() {
        PersistentetService();
        List<RegistrationDto> list = new ArrayList<RegistrationDto>();
        List<RegistrationDto> result = null;
        try {
            Query query = PersistenceManager.newQuery(RegistrationDto.class);
            result = (List<RegistrationDto>) query.execute();
            PersistenceManager.detachCopyAll(result);
            for (RegistrationDto dto : result) {
                list.add(dto);
            }
        } catch (JDOObjectNotFoundException e) {
            // ログ出力
        } finally {
            PersistenceManager.close();
        }
        return list;
    }

    protected void PersistentetService() {
        if (PersistenceManager == null) {
            PersistenceManager = PMF.get().getPersistenceManager();
        }

    }

}
